package pers.liaohaolong.mapzoner;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SaveUtils {

    public static void save(int background, boolean[][] mask, int rgb, String fileName) throws IOException {
        int width = mask[0].length;
        int height = mask.length;
        // 创建新图片
        BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (mask[y][x]) {
                    outputImage.setRGB(x, y, rgb);
                } else {
                    outputImage.setRGB(x, y, background);
                }
            }
        }
        // 保存
        ImageIO.write(outputImage, "png", new File(fileName));
    }

    public static void save(BufferedImage background, boolean[][] mask, int rgb, String fileName) throws IOException {
        int width = mask[0].length;
        int height = mask.length;
        // 复制background
        BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (mask[y][x]) {
                    outputImage.setRGB(x, y, rgb);
                } else {
                    outputImage.setRGB(x, y, background.getRGB(x, y));
                }
            }
        }
        // 保存
        ImageIO.write(outputImage, "png", new File(fileName));
    }

}
